package com.sunda.spmsorder.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunda.spmsorder.entity.OrderLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单审批记录表 Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2021-02-03
 */
@Component
@Mapper
public interface OrderLogMapper extends BaseMapper<OrderLog> {

    @Insert("INSERT INTO ORDER_LOG (WORK_CODE, COMMENTS, UUID) VALUES (#{userId}, #{comments}, #{uuid})")
    void insertOrderLog(@Param("userId") String userId, @Param("comments") String comments, @Param("uuid") String uuid);

    @Select("SELECT ol.UUID AS \"uuid\",\n" +
            "       ow.SPMS_WS_ID AS \"spmsWsId\",\n" +
            "       ol.WORK_CODE AS \"userId\",\n" +
            "       u.LAST_NAME AS \"lastName\",\n" +
            "       ol.COMMENTS AS \"comments\",\n" +
            "       ol.CREATE_TIME AS \"createTime\",\n" +
            "       ol.LOG_AUTO_ID AS \"logAutoId\"\n" +
            "FROM ORDER_LOG ol\n" +
            "    LEFT JOIN ORDER_WORKSHOP ow ON ow.UUID = ol.UUID \n" +
            "    LEFT JOIN SPMS_USER u ON ol.WORK_CODE = u.WORK_CODE\n" +
            "WHERE ow.SPMS_WS_ID = #{spmsWsId} ORDER BY ol.LOG_AUTO_ID DESC\n")
    List<Map<String, Object>> getLogByOrderWsId(@Param("spmsWsId") String spmsWsId);


    @Select("SELECT ol.UUID AS \"uuid\",\n" +
            "       ow.SPMS_ID AS \"spmsId\",\n" +
            "       ol.WORK_CODE AS \"userId\",\n" +
            "       u.LAST_NAME AS \"lastName\",\n" +
            "       ol.COMMENTS AS \"comments\",\n" +
            "       ol.CREATE_TIME AS \"createTime\",\n" +
            "       ol.LOG_AUTO_ID AS \"logAutoId\"\n" +
            "FROM ORDER_LOG ol\n" +
            "    LEFT JOIN ORDER_WERKS ow ON ow.UUID = ol.UUID\n" +
            "    LEFT JOIN SPMS_USER u ON ol.WORK_CODE = u.WORK_CODE\n" +
            "WHERE ow.SPMS_ID = #{spmsId} ORDER BY ol.LOG_AUTO_ID DESC")
    List<Map<String, Object>> getLogByOrderWerksId(@Param("spmsId") String spmsId);

    @Select("SELECT DISTINCT W.UUID AS \"orderWerksUuid\",\n" +
            "                W.SPMS_ID AS \"orderWerksId\",\n" +
            "                W.STATUS AS \"orderWerksStatus\",\n" +
            "                WD.ROW_ID_WERKS AS \"orderWerksRowId\",\n" +
            "                OW.UUID AS \"orderWorkshopUuid\",\n" +
            "                OW.SPMS_WS_ID AS \"orderWorkshopId\",\n" +
            "                OWD.ROW_ID AS \"orderWorkshopRowId\",\n" +
            "                OWD.MATERIAL_NO AS \"materialNo\",\n" +
            "                OWD.MATERIAL_ZH_DESC AS \"materialZhDesc\",\n" +
            "                OWD.APPROVAL_QTY AS \"approvalQty\",\n" +
            "                OWD.BASIC_UNIT AS \"basicUnit\"\n" +
            "FROM ORDER_WORKSHOP OW\n" +
            "    LEFT JOIN ORDER_WORKSHOP_DTL OWD ON OW.UUID = OWD.UUID\n" +
            "    LEFT JOIN ORDER_WERKS_DTL WD ON WD.SPMS_WS_ID = OWD.SPMS_WS_ID AND WD.ROW_ID_WS = OWD.ROW_ID\n" +
            "    LEFT JOIN ORDER_WERKS W ON WD.UUID = W.UUID WHERE W.UUID = #{orderWerksUuid} \n")
    List<Map<String, Object>> getOrderWerksOrderLogSrm(@Param("orderWerksUuid") String orderWerksUuid);

    @Select("SELECT DISTINCT W.UUID AS \"orderWerksUuid\",\n" +
            "                W.SPMS_ID AS \"orderWerksId\",\n" +
            "                W.STATUS AS \"orderWerksStatus\",\n" +
            "                WD.ROW_ID_WERKS AS \"orderWerksRowId\",\n" +
            "                OW.UUID AS \"orderWorkshopUuid\",\n" +
            "                OW.SPMS_WS_ID AS \"orderWorkshopId\",\n" +
            "                OWD.ROW_ID AS \"orderWorkshopRowId\",\n" +
            "                OWD.MATERIAL_NO AS \"materialNo\",\n" +
            "                OWD.MATERIAL_ZH_DESC AS \"materialZhDesc\",\n" +
            "                OWD.APPROVAL_QTY AS \"approvalQty\",\n" +
            "                OWD.BASIC_UNIT AS \"basicUnit\"\n" +
            "FROM ORDER_WORKSHOP OW\n" +
            "         LEFT JOIN ORDER_WORKSHOP_DTL OWD ON OW.UUID = OWD.UUID\n" +
            "         LEFT JOIN ORDER_WERKS_DTL WD ON WD.SPMS_WS_ID = OWD.SPMS_WS_ID AND WD.ROW_ID_WS = OWD.ROW_ID\n" +
            "         LEFT JOIN ORDER_WERKS W ON WD.UUID = W.UUID WHERE OW.UUID = #{orderWorkshopUuid} ")
    List<Map<String, Object>> getOrderWorkshopOrderLogSrm(@Param("orderWorkshopUuid") String orderWorkshopUuid);

    @Select("SELECT ROWNUM * 10 AS \"rowId\", RE.*\n" +
            "FROM (SELECT L.WORK_CODE AS \"userId\",\n" +
            "             L.COMMENTS AS \"comments\",\n" +
            "             L.UUID AS \"uuid\",\n" +
            "             L.LOG_AUTO_ID AS \"logAutoId\",\n" +
            "             L.CREATE_TIME AS \"createTime\",\n" +
            "             U.LAST_NAME AS \"username\"\n" +
            "      FROM ORDER_LOG L\n" +
            "               LEFT JOIN SPMS_USER U ON L.WORK_CODE = U.WORK_CODE\n" +
            "      WHERE L.UUID = #{uuid}  \n" +
            "      ORDER BY L.LOG_AUTO_ID DESC) RE ")
    List<Map<String, Object>> getOrderLogList(@Param("uuid") String uuid);
}
